home *** CD-ROM | disk | FTP | other *** search
- /* File: serial.c
- *
- * Developer: Jim Logan
- * Organization: Brigham Young University
- * Email: loganj@byuvax.bitnet
- * loganj@yvax.byu.edu
- *
- * Captures incoming data from the active serial port and
- * provides control of serial port characteristics (baud
- * rate, parity, stop bits, handshaking ... ).
- *
- */
-
- #include <Quickdraw.h>
- #include <Devices.h>
- #include <Files.h>
- #include <Serial.h>
- #include <Dialogs.h>
- #include <Memory.h>
- #include <Packages.h>
- #include <Resources.h>
- #include <stdio.h>
- #include <strings.h>
- #include <ToolUtils.h>
-
- #include "ConfigRec.h"
- #include "whatami.h"
- #include "wind.h"
-
- #include "Maclook.h"
- #include "Slip.h"
-
- #define input_size 8192
-
- #define SPEED_RESOURCE_ID 23231
- #define DATABITS_RESOURCE_ID 23232
- #define PARITY_RESOURCE_ID 23233
- #define STOPBITS_RESOURCE_ID 23234
- #define CHOOSEPORT_RESOURCE_ID 23235
- #define HANDSHAKE_RESOURCE_ID 23236
-
- extern int numwindows;
-
- static ResType
- Setres = 'CNFG';
-
- static unsigned char *minputnam[] = {"\p.ain","\p.bin"};
- static unsigned char *moutputnam[] = {"\p.aout","\p.bout"};
-
- #if 0
- static unsigned char *mbtext[] = {
- "\p300 baud","\p600 baud","\p1200 baud","\p1800 baud","\p2400 baud",
- "\p3600 baud","\p4800 baud","\p7200 baud","\p9600 baud",
- "\p19200 baud","\p57600 baud", 0};
- static unsigned char *mdtext[] = {"\p5 data bits","\p6 data bits","\p7 data bits","\p8 data bits", 0};
- static unsigned char *mptext[] = {"\pNo parity","\pOdd parity","\pEven parity", 0};
- static unsigned char *mstext[] = {"\p1 stop bit","\p1.5 stop bits","\p2 stop bits", 0};
- static unsigned char *mPorttext[] = {"\pModem Port","\pPrinter Port", 0};
- static unsigned char *mprottext[] = {"\pNo handshaking","\pXon/Xoff Handshaking", 0};
- #endif
-
- #if 0
- #define baud300 380
- #define baud600 189
- #define baud1200 94
- #define baud1800 62
- #define baud2400 46
- #define baud3600 30
- #define baud4800 22
- #define baud7200 14
- #define baud9600 10
- #define baud19200 4
- #define baud57600 0
- #define stop10 16384
- #define stop15 -32768
- #define stop20 -16384
- #define noParity 0
- #define oddParity 4096
- #define evenParity 12288
- #define data5 0
- #define data6 2048
- #define data7 1024
- #define data8 3072
- #define sPortA 0
- #define sPortB 1
- #endif
-
- static int mbtable[] =
- { baud300, baud600,
- baud1200,baud1800,
- baud2400,baud3600,
- baud4800,baud7200,
- baud9600,baud19200,baud57600};
-
- static int mdtable[] = {data5,data6,data7,data8};
- static int mptable[] = {noParity,oddParity,evenParity};
- static int mstable[] = {stop10,stop15,stop20};
- static int minputnum[] = {-6,-8}; /* File numbers of incoming modem/printer ports */
- static int moutputnum[] = {-7,-9}; /* File numbers of outgoing modem/printer ports */
- static unsigned char mramnum[] = {sPortA, sPortB};
-
- static SerShk shakies =
- { (char)0,
- (char)0,
- (char)17, /* "xon" character is control q */
- (char)19, /* "xoff" character is control s */
- (char)0,
- (char)0,
- (char)1, /* "fInX" field: enable incoming "xon/xoff" handshaking */
- (char)0};
-
- unsigned char
- SLIP_ip_number[4],
- DTRleaveon = 0x80,
- DTRturnoff = 0x80;
-
- short
- serial_scrn=-1,
- slip_connection=0, /* 0 = serial, 1 = slip */
- mpi, /* Modem input file refnum */
- mpo, /* Modem output file refnum */
- mbaud, /* Port speed */
- mdata, /* Port data bits */
- mparity, /* Port parity */
- mport, /* Port selected */
- mprot, /* Port protocol */
- mstop; /* Port stop bits */
-
- char
- *modembuf,
- *incoming;
-
- extern WindRec *screens;
-
-
- /* Set the program settings from the "CNFG" resource */
-
- SetSettings(rp) unsigned short *rp; {
- unsigned short *resptr;
- unsigned short i;
- resptr = rp;
- mbaud = *(resptr++);
- mdata = *(resptr++);
- mparity = *(resptr++);
- mport = *(resptr++);
- mprot = *(resptr++);
- mstop = *(resptr++);
- i = *(resptr++);
- SLIP_ip_number[0] = (i >> 8) & 0xff;
- SLIP_ip_number[1] = i & 0xff;
- i = *(resptr++);
- SLIP_ip_number[2] = (i >> 8) & 0xff;
- SLIP_ip_number[3] = i & 0xff;
- }
-
-
- /* Get the (program settings) "CNFG" resource */
-
- getSettings() {
- char **myResource; unsigned short *resptr;
- #define SettingsID 31324
-
- myResource = (char**) GetResource( Setres, SettingsID);
- resptr = (unsigned short*) *myResource;
- SetSettings(resptr);
- ReleaseResource(myResource);
- }
-
-
- /* Save the program settings in the "CNFG" resource */
-
- saveSettings() {
- unsigned short *resptr;
- char **myResource;
- GrafPtr tempPort;
-
- GetPort(&tempPort);
- myResource = (char**) GetResource( Setres, SettingsID);
- resptr = (unsigned short*) *myResource;
- *(resptr++) = mbaud;
- *(resptr++) = mdata;
- *(resptr++) = mparity;
- *(resptr++) = mport;
- *(resptr++) = mprot;
- *(resptr++) = mstop;
- *(resptr++) = (SLIP_ip_number[0] << 8) | SLIP_ip_number[1];
- *(resptr++) = (SLIP_ip_number[2] << 8) | SLIP_ip_number[3];
-
- ChangedResource(myResource);
- WriteResource(myResource);
- ReleaseResource(myResource);
- SetPort(tempPort);
- }
-
- /* Open a serial port, use the "incoming" buffer, and
- set "xon/xoff" handshaking on */
-
- PortOpen() {
- short fstatus,mSettings;
- #if 0
- fstatus = RAMSDOpen(mramnum[mport]);
- #endif
-
- fstatus = OpenDriver(minputnam[mport],&mpi);
- fstatus = OpenDriver(moutputnam[mport],&mpo);
-
- mSettings = mbtable[mbaud] + mdtable[mdata] + mptable[mparity] + mstable[mstop];
- fstatus = SerReset(mpi,mSettings);
- fstatus = SerReset(mpo,mSettings);
- fstatus = SerSetBuf(mpi,incoming,input_size); /* Input buffer specification */
- shakies.fInX = (char) mprot;
- fstatus = SerHShake(mpi,&shakies); /* Handshake parameters */
- Control(mpo,16,(const void *) &DTRleaveon); /* Leave DTR alone on close */
- Control(mpo,17,(const void *) 0); /* Enable DTR */
- }
-
-
- /* Close the serial port */
-
- PortClose() {
- short fstatus;
- if (mpi != 0) {
- fstatus = CloseDriver(mpi);
- mpi = 0;
- }
- if (mpo != 0) {
- fstatus = CloseDriver(mpo);
- mpo = 0;
- }
- #if 0
- RAMSDClose(mramnum[mport]);
- #endif
- }
-
-
- /* This function checks to see if there is any data coming into
- the serial port, and if so captures the data. */
-
- DoModem() {
- OSErr mpistatus; /* mpi is modem.port.in */
- long fcount,mpicount;
- int fstatus;
-
- if (serial_scrn >= 0) {
- mpistatus = SerGetBuf(mpi, &mpicount); /* How much data is coming? */
- if ((mpistatus == noErr) && (mpicount > 0)) { /* Number of characters. */
-
- fcount = mpicount;
- fstatus = FSRead(mpi, &fcount, modembuf); /* Read all of the data. */
- if (slip_connection)
- SLIPreceive(modembuf, (int) fcount);
- else
- parse(&screens[serial_scrn], modembuf, (int) fcount);
- } } }
-
-
- /* Paint a wide border around button #1 to indicate which
- button is the default button */
-
- PaintDial(myDialog) /* Circle OK item 1 */
- DialogPtr myDialog; {
- short item_type;
- Handle item_Handle;
- Rect item_box;
-
- SetPort( myDialog);
- GetDItem( myDialog,1,&item_type,&item_Handle,&item_box);
- item_box.left--; item_box.top--; /* TBD: use InsetRect() */
- item_box.right++; item_box.bottom++;
- PenSize(3,3);
- InsetRect( &item_box,-3,-3);
- FrameRoundRect( &item_box,16,16);
- }
-
-
- /* GetDIHandle - return handle value of dialog item */
-
- Handle GetDIHandle(theDia,i) DialogPtr theDia;int i; {
- short theType;
- Handle theHandle;
- Rect theRect;
- GetDItem(theDia, i, &theType, &theHandle, &theRect);
- return theHandle;
- }
-
-
- /* Restrict the value of an integer to between "lo" and "hi" */
-
- int BoundsLimitWithWrapAround( vPtr, lo, hi) short *vPtr, lo, hi; { /* BYU 2.4.15 MPW */
- if( *vPtr < lo) *vPtr = hi;
- else if( *vPtr > hi) *vPtr = lo;
- return( *vPtr);
- }
-
-
- /* Set the characteristics of the serial port */
-
- Setmodem() {
- GrafPtr tempPort;
- short the_item,tbaud,tdata,tparity,tport,tprot,tstop;
- DialogPtr myDialog;
- unsigned char TEMP_ip_number[4];
- Str255 item_text;
-
- InitCursor();
- GetPort(&tempPort);
- #define MODEM_DLOD_ID 28243
-
- myDialog = GetNewDialog( MODEM_DLOD_ID,(DialogPeek) 0L, (WindowPtr)-1L);
- PaintDial(myDialog);
-
- GetIndString(item_text,SPEED_RESOURCE_ID,mbaud+1);
- SetIText( GetDIHandle( myDialog, 15), item_text);
- GetIndString(item_text,DATABITS_RESOURCE_ID,mdata+1);
- SetIText( GetDIHandle( myDialog, 16), item_text);
- GetIndString(item_text,PARITY_RESOURCE_ID,mparity+1);
- SetIText( GetDIHandle( myDialog, 17), item_text);
- GetIndString(item_text,STOPBITS_RESOURCE_ID,mstop+1);
- SetIText( GetDIHandle( myDialog, 18), item_text);
- GetIndString(item_text,CHOOSEPORT_RESOURCE_ID,mport+1);
- SetIText( GetDIHandle( myDialog, 19), item_text);
- GetIndString(item_text,HANDSHAKE_RESOURCE_ID,mprot+1);
- SetIText( GetDIHandle( myDialog, 20), item_text);
- sprintf(item_text,"%d.%d.%d.%d", (int) SLIP_ip_number[0],
- (int) SLIP_ip_number[1], (int) SLIP_ip_number[2],(int) SLIP_ip_number[3]);
- c2pstr((char *) item_text);
- SetIText( GetDIHandle( myDialog, 21), item_text);
- SelIText( myDialog,21,0,32767);
-
- tbaud = mbaud; tdata = mdata; tparity = mparity;
- tport = mport; tprot = mprot; tstop = mstop;
- TEMP_ip_number[0] = SLIP_ip_number[0];
- TEMP_ip_number[1] = SLIP_ip_number[1];
- TEMP_ip_number[2] = SLIP_ip_number[2];
- TEMP_ip_number[3] = SLIP_ip_number[3];
-
- do {
- ModalDialog((long) 0,&the_item);
- if ((the_item == 3) || (the_item == 4) ) { /* UP/DOWN tbaud */
- tbaud += ((the_item == 3 /* UP*/) ? 1 : -1);
- BoundsLimitWithWrapAround( &tbaud, 0, 10);
- GetIndString(item_text,SPEED_RESOURCE_ID,tbaud+1);
- SetIText( GetDIHandle( myDialog, 15), item_text);
- } else if ((the_item == 5) || (the_item == 6)) { /* UP/DOWN tdata */
- tdata += ((the_item == 5 /* UP*/) ? 1 : -1);
- BoundsLimitWithWrapAround( &tdata, 0, 3);
- GetIndString(item_text,DATABITS_RESOURCE_ID,tdata+1);
- SetIText( GetDIHandle( myDialog, 16), item_text);
- } else if ((the_item == 7) || (the_item == 8)) { /* UP/DOWN tparity */
- tparity += ((the_item == 7 /* UP*/) ? 1 : -1);
- BoundsLimitWithWrapAround( &tparity, 0, 2);
- GetIndString(item_text,PARITY_RESOURCE_ID,tparity+1);
- SetIText( GetDIHandle( myDialog, 17), item_text);
- } else if ((the_item == 9) || (the_item == 10)) { /* UP/DOWN tstop */
- tstop += ((the_item == 9 /* UP*/) ? 1 : -1);
- BoundsLimitWithWrapAround( &tstop, 0, 2);
- GetIndString(item_text,STOPBITS_RESOURCE_ID,tstop+1);
- SetIText( GetDIHandle( myDialog, 18), item_text);
- } else if ((the_item == 11) || (the_item == 12)) { /* UP/DOWN tport */
- tport += ((the_item == 11 /* UP*/) ? 1 : -1);
- BoundsLimitWithWrapAround( &tport, 0, 1);
- GetIndString(item_text,CHOOSEPORT_RESOURCE_ID,tport+1);
- SetIText( GetDIHandle( myDialog, 19), item_text);
- } else if ((the_item == 13) || (the_item == 14)) { /* UP/DOWN tpROt */
- tprot += ((the_item == 13 /* UP*/) ? 1 : -1);
- BoundsLimitWithWrapAround( &tprot, 0, 1);
- GetIndString(item_text,HANDSHAKE_RESOURCE_ID,tprot+1);
- SetIText( GetDIHandle( myDialog, 20), item_text);
- } else if (the_item == 21) { /* SLIP IP number */
- GetIText( GetDIHandle( myDialog, 21), item_text);
- p2cstr((char *) item_text);
- decodeIPnum(item_text,&TEMP_ip_number[0]);
- }
- } while ((the_item != ok) && (the_item != cancel)); /* BYU 2.4.15 MPW */
-
- if (the_item == ok) /* BYU 2.4.15 MPW - Save temps to theRealThings */
- {
- PortClose();
- mbaud = tbaud; mdata = tdata; mparity = tparity;
- mport = tport; mprot = tprot; mstop = tstop;
- SLIP_ip_number[0] = TEMP_ip_number[0];
- SLIP_ip_number[1] = TEMP_ip_number[1];
- SLIP_ip_number[2] = TEMP_ip_number[2];
- SLIP_ip_number[3] = TEMP_ip_number[3];
- saveSettings();
- PortOpen();
- }
- CloseDialog( myDialog);
- SetPort(tempPort);
- return( the_item);
- }
-
- write_serial(buffer,nsend) char *buffer; int nsend; {
- long byte_count;
- int fstatus;
- byte_count = nsend;
- fstatus = FSWrite(mpo,&byte_count,buffer);
- }
-
- /* Initialize serial i/o */
-
- init_serial() {
- long mysize;
- serial_scrn = -1;
- getSettings();
- mysize = input_size;
- modembuf = (char *) NewPtr(mysize);
- incoming = (char *) NewPtr(mysize);
- }
-
- int open_serial(pnum) int pnum; {
- if (serial_scrn < 0) {
- serial_scrn = numwindows;
- PortOpen(); /* Open the serial port */
- return pnum;
- }
- OtherError((char *) "\pError opening serial port.",(char *) "\pSerial port already open."); /* BYU serial */
- return -1;
- }
-
- close_serial() {
- serial_scrn = -1;
- if (mpo != 0)
- PortClose();
- }
-
- serial_shut() {
- serial_scrn = -1;
- if (mpo != 0) {
- Control(mpo,16,(const void *) &DTRturnoff); /* Don't leave DTR alone on close */
- Control(mpo,18,(const void *) 0); /* Disable DTR */
- } }
-